/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 22.07.2005
 *
 *************************************************/
package fightplan.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;

import com.jgoodies.binding.value.ValueModel;

import fightplan.business.Tournament;
import fightplan.core.TournamentMemento;
import fightplan.persistence.FilePersistence;

public class UndoRedoManager {

	private Action undoAction;
	// private Action redoAction;
	private TournamentMemento pastMemento;
	// private TournamentMemento futureMemento;

	private ValueModel tournamentHolder;

	public UndoRedoManager(ValueModel tournamentHolder) {
		this.tournamentHolder = tournamentHolder;

		undoAction = new UndoAction();
		// redoAction = new RedoAction();

		updateActions();
	}

	// public Action getRedoAction() {
	// return redoAction;
	// }

	public Action getUndoAction() {
		return undoAction;
	}

	public void createMemento(String undoText) {
		Tournament tournament = (Tournament) tournamentHolder.getValue();
		if (tournament == null)
			return; // could happen at startup

		TournamentMemento memento = new TournamentMemento(pastMemento, undoText);

		FilePersistence.save(memento.getBeforeFile(), tournament, true);

		pastMemento = memento;
		// futureMemento = null;
		updateActions();
	}

	public void recallMemento() {
		pastMemento = pastMemento.getPrev();
		updateActions();
	}

	// public void completeMemento(String redoText) {
	// Tournament tournament = (Tournament)tournamentHolder.getValue();
	// FilePersistence.save(pastMemento.getAfterFile(), tournament);
	// pastMemento.setRedoText(redoText);
	// }

	private void updateActions() {
		boolean undoable = pastMemento != null;
		// boolean redoable = futureMemento != null;

		if (undoable) {
			undoAction.putValue(Action.NAME, "Undo: " + pastMemento.getUndoText());
		} else {
			undoAction.putValue(Action.NAME, "Undo");
		}

		// redoable = futureMemento != null;
		// if (redoable) {
		// redoAction.putValue(Action.NAME, futureMemento.getRedoText());
		// }

		undoAction.setEnabled(undoable);
		// redoAction.setEnabled(redoable);
	}

	@SuppressWarnings("serial")
	private class UndoAction extends AbstractAction {
		public UndoAction() {
			super("Undo");
			putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Z', KeyEvent.CTRL_DOWN_MASK));
		}

		public void actionPerformed(ActionEvent e) {
			try {
				Tournament undoTournament = FilePersistence.load(pastMemento.getBeforeFile(), true);

				tournamentHolder.setValue(undoTournament);
				// futureMemento = pastMemento;
				pastMemento = pastMemento.getPrev();
				updateActions();
			} catch (Exception x) {
				x.printStackTrace();
			}

		}

	}

	/*
	 * @SuppressWarnings("serial") private class RedoAction extends
	 * AbstractAction { public RedoAction() { super("Redo"); }
	 * 
	 * public void actionPerformed(ActionEvent e) { try { Tournament
	 * redoTournament = FilePersistence.load(futureMemento.getAfterFile());
	 * 
	 * tournamentHolder.setValue(redoTournament); pastMemento = futureMemento;
	 * futureMemento = futureMemento.getNext(); updateActions(); } catch
	 * (Exception x) { x.printStackTrace(); }
	 * 
	 * }
	 * 
	 * }
	 */
}
